<html>
<head>
<title>Employee Sales Result</title>
<style type="text/css">
body {
	font-family:verdana,arial,sans-serif;
	font-size:10pt;
	margin:30px;
	background-color:#ffcc00;
	}
</style>
</head>
<body>
<?php
date_default_timezone_set('UTC');

$username="bsc353_4";
$password="sonbeans";
$database="bsc353_4";

$branchID=$_POST['branchid'];

if ($branchID=="Select One")
{
    echo"Error. Please select an option for the Branch.";
}

else
{

mysql_connect("clipper.encs.concordia.ca",$username,$password) or die ("Unable to connect to database");

@mysql_select_db($database) or die ("Unable to select database");


// Check connection


if ($branchID=='All Of MBS')
{
    $query="SELECT Customer.ID, Customer.name, COUNT(Customer.ID) AS amount_of_cumulative_purchase FROM Customer JOIN Sale ON Customer.ID = Sale.customerID WHERE MONTH(Sale.dateTime) = MONTH(CURDATE())-1 GROUP BY Customer.ID ORDER BY amount_of_cumulative_purchase DESC LIMIT 5";
    //echo "$query";
}
else
{   
    $query="SELECT Customer.ID, Customer.name, COUNT(Customer.ID) AS amount_of_cumulative_purchase FROM Customer JOIN Sale ON Customer.ID = Sale.customerID WHERE MONTH(Sale.dateTime) = MONTH(CURDATE())-1 AND Sale.storeBranchID = '$branchID' GROUP BY Customer.ID ORDER BY amount_of_cumulative_purchase DESC LIMIT 5";
    //echo "$query";
}
$result=mysql_query($query) or die ("failed to find record");
$num=mysql_numrows($result);

//echo "1 record found";
mysql_close();
}
?>

<table border="1" cellspacing="2" cellpadding="2">
<tr>
<td></td>
<td><font face="Arial, Helvetica, sans-serif">Customer ID</font></td>
<td><font face="Arial, Helvetica, sans-serif">Customer Name</font></td>
<td><font face="Arial, Helvetica, sans-serif">Total Purchases</font></td>
</tr>

<?php
$i=0;
while ($i < $num) {

$f1=mysql_result($result,$i,"Customer.ID");
$f2=mysql_result($result,$i,"Customer.name");
$f3=mysql_result($result,$i,"amount_of_cumulative_purchase");

?>

<tr>
<td><?php echo ($i+1); ?>.</td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
</tr>

<?php
$i++;
}
?> 
</body>
</html>